//
//  FCTextView.m
//  辅城
//
//  Created by jianhong zhang on 2017/9/8.
//  Copyright © 2017年 吴 剑川. All rights reserved.
//

#import "FCTextView.h"
#import "NSString+XMGExtension.h"

@interface FCTextView ()<UITextViewDelegate>


@property(nonatomic,weak) UILabel *placeholderLabel;
@property(nonatomic,weak) UILabel *limitNumLabel;

@end

@implementation FCTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        [self initProperties];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if(self = [super initWithCoder:aDecoder]) {
        [self initProperties];
    }
    return self;
}

- (void)initProperties {
        
    UITextView *textView = [[UITextView alloc] init];
    textView.font = [UIFont systemFontOfSize:15];
    textView.delegate = self;
    [self addSubview:textView];
    _textView = textView;
    
    UILabel *placeholderLabel = [[UILabel alloc] init];
    placeholderLabel.textAlignment = NSTextAlignmentLeft;
    placeholderLabel.textColor = [UIColor lightGrayColor];
    placeholderLabel.numberOfLines = 0;
    [self addSubview:placeholderLabel];
    _placeholderLabel = placeholderLabel;
    
    UILabel *limitNumLabel = [[UILabel alloc]init];
    limitNumLabel.textAlignment = NSTextAlignmentRight;
    limitNumLabel.font = [UIFont systemFontOfSize:14.0f];
    limitNumLabel.textColor = [UIColor grayColor];
    [self addSubview:limitNumLabel];
    _limitNumLabel = limitNumLabel;
    
    _limitNum = LONG_MAX;
    _limitNumLabel.hidden = YES;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _textView.frame = CGRectMake(5 , 3, self.frame.size.width - 10, self.frame.size.height - 30);
    _textView.backgroundColor = self.backgroundColor;
    _limitNumLabel.frame = CGRectMake(_textView.frame.origin.x, CGRectGetMaxY(_textView.frame) + 5, _textView.frame.size.width - 5, 20);
    
    UIFont *font = _textView.font;
    if (font==nil) {
        font = [UIFont systemFontOfSize:15];
    }
    UIEdgeInsets edgeInset = _textView.textContainerInset;
    
    CGFloat placeW = self.frame.size.width-(edgeInset.left+5)-(edgeInset.right+5);
    CGFloat placeH = [_placeholderLabel.text sizeWithFont:font maxW:placeW].height;
    _placeholderLabel.textAlignment = _textView.textAlignment;
    _placeholderLabel.font = font;
    _placeholderLabel.frame = CGRectMake(edgeInset.left+10, edgeInset.top+3, placeW, placeH);
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([_delegate respondsToSelector:@selector(textViewShouldBeginEditing:)]) {
        return [_delegate textViewShouldBeginEditing:self];
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if ([_delegate respondsToSelector:@selector(textViewShouldEndEditing:)]) {
        return [_delegate textViewShouldEndEditing:self];
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSMutableString *newtext = [NSMutableString stringWithString:textView.text];
    [newtext replaceCharactersInRange:range withString:text];
    BOOL result = ([newtext length]<=_limitNum);
    
    if ([_delegate respondsToSelector:@selector(textView: shouldChangeTextInRange:replacementText:)]) {
        BOOL delegateResult = [_delegate textView:self shouldChangeTextInRange:range replacementText:text];
        return result&&delegateResult;
    }
    return result;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    if ([_delegate respondsToSelector:@selector(textView: shouldInteractWithURL:inRange:)]) {
        return [_delegate textView:self shouldInteractWithURL:URL inRange:characterRange];
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange {
    if ([_delegate respondsToSelector:@selector(textView: shouldInteractWithTextAttachment:inRange:)]) {
        return [_delegate textView:self shouldInteractWithTextAttachment:textAttachment inRange:characterRange];
    }
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([_delegate respondsToSelector:@selector(textViewDidBeginEditing:)]) {
        [_delegate textViewDidBeginEditing:self];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    if ([_delegate respondsToSelector:@selector(textViewDidEndEditing:)]) {
        [_delegate textViewDidEndEditing:self];
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length > 0) {
        _placeholderLabel.hidden = YES;
    }else {
        _placeholderLabel.hidden = NO;
    }
    
    NSString *str = textView.text;
    NSInteger number = [str length];
    if ((number > _limitNum)&&(textView.markedTextRange == nil)) { //兼容iOS7
        textView.text = [str substringToIndex:NSMaxRange([str rangeOfComposedCharacterSequenceAtIndex:_limitNum-1])];
    }
    _limitNumLabel.text = [NSString stringWithFormat:@"%zd/%ld", _textView.text.length,_limitNum];
    
    if ([_delegate respondsToSelector:@selector(textViewDidChange:)]) {
        [_delegate textViewDidChange:self];
    }
}

- (void)textViewDidChangeSelection:(UITextView *)textView{
    if ([_delegate respondsToSelector:@selector(textViewDidChangeSelection:)]) {
        [_delegate textViewDidChangeSelection:self];
    }
}

#pragma mark - textViewProperties
- (void)setText:(NSString *)text{
    _textView.text = text;
    [self textViewDidChange:_textView];
}

- (NSString *)text{
    return _textView.text;
}

- (void)setFont:(UIFont *)font{
    _textView.font = font;
}

- (UIFont *)font{
    return _textView.font;
}

- (void)setTextColor:(UIColor *)textColor{
    _textView.textColor = textColor;
}

- (UIColor *)textColor{
    return _textView.textColor;
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment{
    _textView.textAlignment = textAlignment;
}

- (NSTextAlignment)textAlignment{
    return _textView.textAlignment;
}

- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset{
    _textView.textContainerInset = textContainerInset;
}

- (UIEdgeInsets)textContainerInset{
    return _textView.textContainerInset;
}

- (void)setLimitNum:(NSInteger)limitNum{
    _limitNum = limitNum;
    if (limitNum == LONG_MAX) {
        _limitNumLabel.hidden = YES;
    }else{
        _limitNumLabel.text = [NSString stringWithFormat:@"%ld/%ld",0,limitNum];
        _limitNumLabel.hidden = NO;
    }
}

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    _placeholderLabel.text = placeholder;
}

@end
